﻿namespace Example.Structural.Flyweight
{
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;

    public sealed class Spreadsheet
    {
        private readonly Collection<Cell> _cells;

        public Spreadsheet()
        {
            _cells = new Collection<Cell>();
        }

        public Cell this[string column, 
                         int row]
        {
            get
            {
                var value = _cells
                    .Where(x => string.Equals(x.Column, column, StringComparison.OrdinalIgnoreCase) && Equals(x.Row, row))
                    .FirstOrDefault();

                if (null == value)
                {
                    value = new Cell(column.ToUpperInvariant(), row);
                    _cells.Add(value);
                }

                return value;
            }
        }
    }
}